Skip to content

Conversation

@hyanwong
Copy link
Member

@hyanwong hyanwong commented Dec 24, 2025

This confused me, and was the reason that I couldn't canonicalise node 0 such that it pointed to individual 0 (see below and message on slack):

import tskit
print("Tskit version:", tskit.__version__)
ts = tskit.load("canonicalize_issue.trees")
print("Node in orig ts:", ts.node(0))
tables = ts.dump_tables()
tables.canonicalise()
print("Node in canonicalised ts (which I am trying to force with individual id=0):", tables.nodes[0])
ts = tables.tree_sequence()
print("Actual individual 0 in canonicalised has the following nodes:", ts.individual(0).nodes)
Tskit version: 1.0.0
Node in orig ts: Node(id=0, flags=1, time=0.0, population=0, individual=7, metadata=b'')
Node in canonicalised ts (which I am trying to force with individual id=0): NodeTableRow(flags=1, time=0.0, population=0, individual=7, metadata=b'')
Actual individual 0 in canonicalised has the following nodes: [46 47]

@codecov
Copy link

codecov bot commented Dec 24, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.76%. Comparing base (648a595) to head (ccff16e).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3355   +/-   ##
=======================================
  Coverage   89.76%   89.76%           
=======================================
  Files          29       29           
  Lines       31292    31292           
  Branches     5738     5738           
=======================================
  Hits        28089    28089           
  Misses       1794     1794           
  Partials     1409     1409           
Flag Coverage Δ
c-tests 86.77% <ø> (ø)
lwt-tests 80.38% <ø> (ø)
python-c-tests 87.12% <ø> (ø)
python-tests 98.85% <ø> (ø)
python-tests-no-jit 33.51% <ø> (ø)
python-tests-numpy1 50.28% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
python/tskit/tables.py 99.77% <ø> (ø)
python/tskit/trees.py 98.89% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@petrelharp
Copy link
Contributor

Mind pointing to the part(s) of the code from which we know this is accurate?

Copy link
Member

@benjeffery benjeffery left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@hyanwong
Copy link
Member Author

hyanwong commented Jan 4, 2026

Mind pointing to the part(s) of the code from which we know this is accurate?

Well, I think the logic is here, but I'm confused by the num_descendants thing in the C code (do individuals have "descendants" that can be counted?)

qsort(sorted_individuals, (size_t) num_individuals, sizeof(*sorted_individuals),

Either way, we definitely need to sort first by putting parents before children, otherwise the canonical sort will not created tables that meet the individual-table requirements.

@petrelharp
Copy link
Contributor

Well, individuals have a parent attribute, and this is being used to count the number of descendants (as measured within the individual-parent relationships only): see here.

@petrelharp
Copy link
Contributor

The relevant code is here, I guess - looks like it sorts first by number of (individual-parent) descendants, then by referring node ID, then by original order.

@hyanwong hyanwong enabled auto-merge January 6, 2026 08:58
@hyanwong hyanwong added this pull request to the merge queue Jan 6, 2026
Merged via the queue into tskit-dev:main with commit c70f591 Jan 6, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants